using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class CartShippingUpdate : AuthorizedAndSSLPageBaseClass
    {

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            Website = WebsiteData.WebsiteDetail();
            PageID = Website.ShippingUpdateTemplatePageID.Value;
            base.Page_PreInit(sender, e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Page.IsPostBack)
                return;

            CountryDropDownList.DataSource = WebsiteData.LocationCountryList();
            CountryDropDownList.DataBind();
            CountryDropDownList.SelectedValue = "US";

            DataSet AddressNickNameListDS = Soveryn.Commerce.Shared.API.CallForDataSet(CustomerAPIURL, "AddressNickNameList", new
            {
                WebsiteAPIKey = PageBaseClass.WebsiteAPIKey,
                CustomerCartSessionID = CustomerCartSessionID
            }, false, false);


            ShippingAddressDropDownList.DataSource = AddressNickNameListDS;
            ShippingAddressDropDownList.DataBind();
            ShippingAddressDropDownList.Items.Insert(0, new ListItem("Create a new shipping address", "-1"));

            PopulateStateDropDown();

            // Rhett Mike - Fills local shipping addresss properties with that of Customer's
            if (Customer.ShippingAddress != null)
            {

                AddressProperties myCustomerShipAddressProperties = new AddressProperties();

                myCustomerShipAddressProperties.AddressID = Customer.ShippingAddress.AddressID;
                myCustomerShipAddressProperties.CustomerID = Customer.CustomerID;
                myCustomerShipAddressProperties.AddressNickName = Customer.ShippingAddress.AddressNickName;
                myCustomerShipAddressProperties.AddressCompanyName = Customer.ShippingAddress.AddressCompanyName;
                myCustomerShipAddressProperties.AddressFirstName = Customer.ShippingAddress.AddressFirstName;
                myCustomerShipAddressProperties.AddressLastName = Customer.ShippingAddress.AddressLastName;
                myCustomerShipAddressProperties.AddressLine1 = Customer.ShippingAddress.AddressLine1;
                myCustomerShipAddressProperties.AddressLine2 = Customer.ShippingAddress.AddressLine2;
                myCustomerShipAddressProperties.AddressCity = Customer.ShippingAddress.AddressCity;
                myCustomerShipAddressProperties.AddressStateProvIDPostalAbrev = Customer.ShippingAddress.AddressStateProvIDPostalAbrev;
                myCustomerShipAddressProperties.AddressPostalCode = Customer.ShippingAddress.AddressPostalCode;
                myCustomerShipAddressProperties.AddressCountryIDPostalAbrev = Customer.ShippingAddress.AddressCountryIDPostalAbrev;
                myCustomerShipAddressProperties.AddressPhone = Customer.ShippingAddress.AddressPhone;
                myCustomerShipAddressProperties.AddressFax = Customer.ShippingAddress.AddressFax;

                LoadShippingAddress(myCustomerShipAddressProperties);
            }
        }

        protected void LoadShippingAddress(AddressProperties myShipAddressProperties)
        {

            CountryDropDownList.SelectedValue = myShipAddressProperties.AddressCountryIDPostalAbrev;
            PopulateStateDropDown();

            if (!String.IsNullOrEmpty(myShipAddressProperties.AddressStateProvIDPostalAbrev))
                CartShipStateProvIDCodeDropDown.SelectedValue = myShipAddressProperties.AddressStateProvIDPostalAbrev;

            ShippingAddressDropDownList.SelectedValue = myShipAddressProperties.AddressID;
            CartShipCompanyNameTextBox.Text = myShipAddressProperties.AddressCompanyName;
            CartShipFirstNameTextBox.Text = myShipAddressProperties.AddressFirstName;
            CartShipLastNameTextBox.Text = myShipAddressProperties.AddressLastName;
            CartShipAddress1TextBox.Text = myShipAddressProperties.AddressLine1;
            CartShipAddress2TextBox.Text = myShipAddressProperties.AddressLine2;
            CartShipCityTextBox.Text = myShipAddressProperties.AddressCity;
            CartShipPostalCodeTextBox.Text = myShipAddressProperties.AddressPostalCode;
            CartShipPhoneTextBox.Text = myShipAddressProperties.AddressPhone;
            CartShipFaxTextBox.Text = myShipAddressProperties.AddressFax;
            CartShipNickNameTextBox.Text = myShipAddressProperties.AddressNickName;

        }

        protected void ShippingAddressDropDownList_SelectedIndexChanged(Object sender, System.EventArgs e) //Handles ShippingAddressDropDownList.SelectedIndexChanged
        {

            if (ShippingAddressDropDownList.SelectedIndex > 0)
            {


                DataSet AddressDS = Soveryn.Commerce.Shared.API.CallForDataSet(CustomerAPIURL, "AddressDetails", new
                {
                    WebsiteAPIKey = PageBaseClass.WebsiteAPIKey,
                    CustomerCartSessionID = CustomerCartSessionID,
                    AddressID = ShippingAddressDropDownList.SelectedValue
                }, false, false);

                AddressProperties myProperties = AddressProperties.Create(AddressDS);
                LoadShippingAddress(myProperties);
                DeleteAddressLinkButton.Visible = true;
            }
            else
            {

                DeleteAddressLinkButton.Visible = false;
                CartShipNickNameTextBox.Text = "My Shipping Address";
                CartShipCompanyNameTextBox.Text = "";
                CartShipFirstNameTextBox.Text = "";
                CartShipLastNameTextBox.Text = "";
                CartShipAddress1TextBox.Text = "";
                CartShipAddress2TextBox.Text = "";
                CartShipCityTextBox.Text = "";
                CartShipPostalCodeTextBox.Text = "";
                CartShipPhoneTextBox.Text = "";
                CartShipNickNameTextBox.Text = "";
                CartShipNickNameTextBox.Text = "";
                CartShipFaxTextBox.Text = "";
                CountryDropDownList.SelectedValue = "US";
                PopulateStateDropDown();
                CartShipStateProvIDCodeDropDown.SelectedValue = "-1";
            }

        }

        protected void UpdateShippingInfoButton_Click(Object sender, EventArgs e)
        {


            String AddressID = null;
            if (ShippingAddressDropDownList.SelectedValue != "-1")
                AddressID = ShippingAddressDropDownList.SelectedValue;

            String AddressStateProvIDPostalAbrev = null;
            if (StateRow.Visible)
                AddressStateProvIDPostalAbrev = CartShipStateProvIDCodeDropDown.SelectedValue;

            Soveryn.Commerce.Shared.API.Call(CustomerAPIURL, "ShippingAddressAddUpdate", new
            {

                WebsiteAPIKey = PageBaseClass.WebsiteAPIKey,
                CustomerCartSessionID = CustomerCartSessionID,
                AddressID = AddressID,
                AddressNickName = CartShipNickNameTextBox.Text,
                AddressCompanyName = CartShipCompanyNameTextBox.Text,
                AddressFirstName = CartShipFirstNameTextBox.Text,
                AddressLastName = CartShipLastNameTextBox.Text,
                AddressLine1 = CartShipAddress1TextBox.Text,
                AddressLine2 = CartShipAddress2TextBox.Text,
                AddressCity = CartShipCityTextBox.Text,
                AddressPostalCode = CartShipPostalCodeTextBox.Text,
                AddressCountryIDPostalAbrev = CountryDropDownList.SelectedValue,
                AddressPhone = CartShipPhoneTextBox.Text,
                AddressFax = CartShipFaxTextBox.Text,
                AddressStateProvIDPostalAbrev = AddressStateProvIDPostalAbrev


            }, false, false);


            Response.Redirect("/OrderCheckoutPROC.aspx", true);

        }

        protected void CountryDropDownList_SelectedIndexChanged(Object sender, System.EventArgs e)
        {

            PopulateStateDropDown();

        }

        protected void PopulateStateDropDown()
        {
            CartShipStateProvIDCodeDropDown.Items.Clear();

            CartShipStateProvIDCodeDropDown.DataSource = WebsiteData.LocationStateProvList(CountryDropDownList.SelectedValue);
            CartShipStateProvIDCodeDropDown.DataBind();
            CartShipStateProvIDCodeDropDown.Items.Insert(0, new ListItem("Select State...", "-1"));

            if (CartShipStateProvIDCodeDropDown.Items.Count == 1)
                StateRow.Visible = false;
            else
                StateRow.Visible = true;

        }

    }
}